﻿Public Class clsGJBinfo
    Public Function save(GJBInfo As entity_GJB_DL_INFO) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            szSql = "INSERT INTO sf.b_gjb_dl_info(factory_id, dl_no, gjb_type, layer_num, wt, "
            szSql &= "status,operator,b_gt_sys_id,support_model,material_code,dl_no_lr,update_time,assembling_date,label_no,exprie_date "
            szSql &= ") VALUES ( ? , ? , ? , ? , ? ,? , ? , ? , ? , ? , ? , (SELECT CURRENT_TIMESTAMP(6)), (SELECT CURRENT_TIMESTAMP(6)),?,(SELECT CURRENT_TIMESTAMP(6) + '15 day'))"
            With GJBInfo
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "TS", .dl_no, .gjb_type, .layer_num, .wt, .status, .Ope, .b_gt_sys_id, .support_model, .material_code, .dl_no_lr, .label_No)
            End With

            '更新骨架板吊篮主档忙闲字段
            szSql = String.Empty
            szSql &= "update sf.t_mst_dl set "
            szSql &= " status  = ? "
            szSql &= " where dl_no = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "1", GJBInfo.dl_no)
            DBacs.CommitTransaction()
            'szSql = "select * from  sf.b_gjb_dl_info where factory_id=? and dl_no=? and status=?"
            'Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, "TS", GJBInfo.dl_no, 0)
            'If dt.Rows.Count > 0 Then
            '    szSql = "update sf.b_gjb_dl_info "
            '    szSql &= " set support_model=?,gjb_type=?,layer_num=?,wt=?,operator=?,dl_no_lr=?,label_No=?,assembling_date=(SELECT CURRENT_TIMESTAMP(6)) "
            '    szSql &= " where factory_id=? and dl_no=? and status=? "
            '    With GJBInfo
            '        rtn = DBacs.ExecuteUpdateSql(szSql, -1, .support_model, .gjb_type, .layer_num, .wt, .Ope, .dl_no_lr, .label_No, "TS", .dl_no, "0")
            '    End With
            'Else

            'End If
        Catch ex As Exception
            rtn = -2
            DBacs.RollbackTransaction()
        End Try
        Return rtn
    End Function

    Public Function save(GJBInfo As entity_GJB_DL_INFO, dt As DataTable) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            szSql = "INSERT INTO sf.b_gjb_dl_info(factory_id, dl_no, gjb_type, layer_num, wt, "
            szSql &= "status,operator,b_gt_sys_id,support_model,material_code,dl_no_lr,update_time,assembling_date,label_no,exprie_date "
            szSql &= ") VALUES ( ? , ? , ? , ? , ? ,? , ? , ? , ? , ? , ? , (SELECT CURRENT_TIMESTAMP(6)), (SELECT CURRENT_TIMESTAMP(6)),?,(SELECT CURRENT_TIMESTAMP(6) + '15 day'))"
            With GJBInfo
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "TS", .dl_no, .gjb_type, .layer_num, .wt, .status, .Ope, .b_gt_sys_id, .support_model, .material_code, .dl_no_lr, .label_No)
            End With

            ' 查询最新添加的ID
            szSql = String.Empty
            szSql &= "SELECT * FROM sf.b_gjb_dl_info order by update_time desc limit 1"
            Dim dttable As DataTable = DBacs.ExecuteSql(szSql, -1)
            For index = 0 To dt.Rows.Count - 1
                szSql = "INSERT INTO sf.b_gjb_dl_info_details(b_gjb_dl_info_sys_id , piece_num, b_gt_sys_id)VALUES(?,?,?)"
                DBacs.ExecuteUpdateSql(szSql, -1, dttable.Rows(0)("sys_id"), dt.Rows(index)("into_box_num"), dt.Rows(index)("b_gt_sys_id"))
            Next

            '更新骨架板吊篮主档忙闲字段
            szSql = String.Empty
            szSql &= "update sf.t_mst_dl set "
            szSql &= " status  = ? "
            szSql &= " where dl_no = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "1", GJBInfo.dl_no)
            DBacs.CommitTransaction()
        Catch ex As Exception
            rtn = -2
            DBacs.RollbackTransaction()
        End Try
        Return rtn
    End Function

    Public Function save(num As String, wt As String, sys_id As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        szSql &= "update sf.b_gjb_dl_info set layer_num = ? , wt = ? where sys_id = ? "
        rtn = DBacs.ExecuteUpdateSql(szSql, -1, num, wt, sys_id)
        Return rtn
    End Function

    Public Function saveDelete(dl_no As String, sys_id As String) As Integer
        Dim szSql As String
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            '执行删除
            szSql = " delete from sf.b_gjb_dl_info where sys_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, sys_id)

            '更新骨架板吊篮主档忙闲字段
            szSql = "update sf.t_mst_dl set status = ? where dl_no = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "0", dl_no)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

    Public Function save(num As String, sys_id As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        szSql &= "update sf.b_gjb_dl_info set layer_num = ? where sys_id = ? "
        rtn = DBacs.ExecuteUpdateSql(szSql, -1, num, sys_id)
        Return rtn
    End Function

    Public Function saveUpdateStatus(sys_id As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        szSql &= "update sf.b_gjb_dl_info set status = ? where sys_id = ? "
        rtn = DBacs.ExecuteUpdateSql(szSql, -1, "2", sys_id)
        Return rtn
    End Function

    Public Function getGjbInfoListByGtSysId(gt_sys_id As String) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT COALESCE(sum(layer_num), '0') as layer_num_all FROM sf.b_gjb_dl_info WHERE b_gt_sys_id = ?"
        Return DBacs.ExecuteSql(szSql, -1, gt_sys_id)
    End Function

    Public Function getGjbInfoDetailsListByGtSysId(gt_sys_id As String) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT COALESCE(sum(piece_num), '0') as piece_num_all FROM sf.b_gjb_dl_info_details WHERE b_gt_sys_id = ?"
        Return DBacs.ExecuteSql(szSql, -1, gt_sys_id)
    End Function

    Public Function getListByGtSysId(gt_sys_id As String) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT * FROM sf.b_gjb_dl_info WHERE b_gt_sys_id = ?"
        Return DBacs.ExecuteSql(szSql, -1, gt_sys_id)
    End Function

    Public Sub setdefaultValue(dl_no As String, ByRef txtNum As Object, ByRef txtWeight As Object, Optional xh As Object = Nothing, Optional sys_id As Object = Nothing, Optional txtLabelNo As Object = Nothing)
        Dim szSql As String = String.Empty
        szSql &= "select * from  sf.b_gjb_dl_info where factory_id =? And dl_no_lr=? and status=?	 "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, "TS", dl_no, "0")
        If dt.Rows.Count > 0 Then
            txtNum.Text = dt.Rows(0)("layer_num")
            txtWeight.Text = dt.Rows(0)("wt")
            If IsNothing(xh) = False Then
                xh.text = dt.Rows(0)("gjb_type")
            End If
            If IsNothing(sys_id) = False Then
                sys_id.text = dt.Rows(0)("sys_id")
            End If
            If IsNothing(txtLabelNo) = False Then
                txtLabelNo.text = dt.Rows(0)("dl_no")
            End If
        End If

    End Sub

    Public Function getGjbInfoByBarCode(barCode As String) As DataTable
        Dim szSql As String = String.Empty
        szSql &= "select * from  sf.b_gjb_dl_info where factory_id =? And dl_no=? and status=? "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, "TS", barCode, "0")
        Return dt
    End Function

    Public Function getGjbInfoListByStatus(status As String) As DataTable
        Dim szSql As String = "SELECT * FROM sf.b_gjb_dl_info WHERE status = ?"
        Return DBacs.ExecuteSql(szSql, -1, status)
    End Function

    Public Function getListByDlNo(dl_no As String) As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT a.*,b.material_code as gjb_material_code from sf.b_gjb_dl_info a left join sf.b_gt b on a.b_gt_sys_id = b.sys_id where a.factory_id = ? And a.dl_no_lr = ? order by assembling_date desc"
        Return DBacs.ExecuteSql(szSql, -1, "TS", dl_no)
    End Function

    Public Function getListsByDlNo(dl_no As String) As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT a.*,b.material_code as gjb_material_code from sf.b_gjb_dl_info a left join sf.b_gt b on a.b_gt_sys_id = b.sys_id where a.factory_id = ? And a.dl_no = ? order by assembling_date desc"
        Return DBacs.ExecuteSql(szSql, -1, "TS", dl_no)
    End Function

End Class
